package org.koyabr.webx.demo.danmaku.web.ajax.module.screen;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.citrus.turbine.Context;

import org.koyabr.webx.demo.danmaku.biz.PostManager;
import org.koyabr.webx.demo.danmaku.biz.UserManager;
import org.koyabr.webx.demo.danmaku.dal.dataobjs.Post;
import org.koyabr.webx.demo.danmaku.dal.dataobjs.User;
import org.koyabr.webx.demo.danmaku.web.common.DanmakuUser;
import org.springframework.beans.factory.annotation.Autowired;

public class Favor {
	
	@Autowired
	private PostManager postManager;
	
	@Autowired
	private UserManager userManager;

	@Autowired
	private HttpServletRequest request;

	public void execute(Context context) throws Exception {
		String postId = request.getParameter("postId");
		Post post;
		if (postId == null
				|| (post = postManager.getPostById(postId)) == null)
			return;

		User currentUser = userManager.getUser(DanmakuUser.getCurrentUser()
				.getUsername());
		if(currentUser == null)
			return;
		
		if(currentUser.favorSet.contains(post)){
			currentUser.favorSet.remove(post);
		}
		else {
			currentUser.favorSet.add(post);
		}
		
		context.put("favorNum", post.favorSet.size());
	}
}
